﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tank.Properties;

namespace Tank
{
    class GameObjectManager
    {
        static List<NotMoveing> wallList = new List<NotMoveing>();// 墙的集合
        static List<NotMoveing> seteelList = new List<NotMoveing>();// 银墙集合
        static List<NotMoveing> bossList = new List<NotMoveing>();// boss集合
        private static List<EnemyTank> tankList = new List<EnemyTank>();//敌放坦克
        private static List<Bullet> bulletList = new List<Bullet>();//子弹集合
        private static List<Explosion> expList = new List<Explosion>();
        static MyTank p; //我方坦克 
        private static int enemyBornSpeed = 60;
        private static int enemyBornCount = 60;
        private static Point[] points = new Point[3];
        // 锁
        private static Object _bulletLock = new object();
        // 将墙的实例放到集合中统一保管
        public static void CreateMap()
        {
            CreateWall(1, 1, 5, wallList, Properties.Resources.wall);
            CreateWall(3, 1, 5, wallList, Properties.Resources.wall);
            CreateWall(5, 1, 4, wallList, Properties.Resources.wall);
            CreateWall(7, 1, 3, wallList, Properties.Resources.wall);
            CreateWall(9, 1, 4, wallList, Properties.Resources.wall);
            CreateWall(11, 1, 5, wallList, Properties.Resources.wall);
            CreateWall(13, 1, 5, wallList, Properties.Resources.wall);
            CreateWall(7, 5, 1, seteelList, Properties.Resources.steel);
            CreateWall(0, 7, 1, seteelList, Properties.Resources.steel);

            CreateWall(2, 7, 1, wallList, Properties.Resources.wall);
            CreateWall(3, 7, 1, wallList, Properties.Resources.wall);
            CreateWall(4, 7, 1, wallList, Properties.Resources.wall);
            CreateWall(6, 7, 1, wallList, Properties.Resources.wall);
            CreateWall(7, 6, 2, wallList, Properties.Resources.wall);
            CreateWall(8, 7, 1, wallList, Properties.Resources.wall);
            CreateWall(10, 7, 1, wallList, Properties.Resources.wall);
            CreateWall(11, 7, 1, wallList, Properties.Resources.wall);
            CreateWall(12, 7, 1, wallList, Properties.Resources.wall);
            CreateWall(14, 7, 1, seteelList, Properties.Resources.steel);
            CreateWall(1, 9, 5, wallList, Properties.Resources.wall);
            CreateWall(3, 9, 5, wallList, Properties.Resources.wall);
            CreateWall(5, 9, 3, wallList, Properties.Resources.wall);
            CreateWall(9, 9, 3, wallList, Properties.Resources.wall);
            CreateWall(11, 9, 5, wallList, Properties.Resources.wall);
            CreateWall(13, 9, 5, wallList, Properties.Resources.wall);
            CreateWall(6, 10, 1, wallList, Properties.Resources.wall);
            CreateWall(7, 10, 2, wallList, Properties.Resources.wall);
            CreateWall(8, 10, 1, wallList, Properties.Resources.wall);

            CreateWall(6, 13, 2, wallList, Properties.Resources.wall);
            CreateWall(7, 13, 1, wallList, Properties.Resources.wall);
            CreateWall(8, 13, 2, wallList, Properties.Resources.wall);
            CreateBoss(7, 14, bossList, Properties.Resources.Boss);

        }
        // 当坦克更新的时候调用
        public static void Update()
        {
            foreach (NotMoveing i in wallList)
            {

                i.Update();
            }
            foreach (NotMoveing i in seteelList)
            {

                i.Update();
            }
            foreach (NotMoveing i in bossList)
            {

                i.Update();
            }
            foreach (EnemyTank i in tankList)
            {
                i.Update();
            }
            CheckAndDestroyBullet();
            lock (_bulletLock)
            {
                foreach (Bullet i in bulletList)
                {

                    i.Update();
                }
            }
            foreach (Explosion i in expList)
            {
                i.Update();
            }
            p.Update();
            EnemyBorn();
           CheckAndDestroyExplosin();


        }
        // 开始生产坦克的位置
        public static void Start()
        {
            points[0].X = 0;
            points[0].Y = 0;
            points[1].X = 7 * 30;
            points[1].Y = 0;
            points[2].X = 14 * 30;
            points[2].Y = 0;

        }
        private static void CreateEnemyTank1(int x, int y)
        {
            EnemyTank tank = new EnemyTank(x, y, 2, Resources.GrayDown,
                Resources.GrayLeft, Resources.GrayRight, Resources.GrayUp
                );
            tankList.Add(tank);

        }
        private static void CreateEnemyTank2(int x, int y)
        {
            EnemyTank tank = new EnemyTank(x, y, 2, Resources.GreenDown,
    Resources.GreenLeft, Resources.GreenRight, Resources.GreenUp
    );
            tankList.Add(tank);
        }
        private static void CreateEnemyTank3(int x, int y)
        {
            EnemyTank tank = new EnemyTank(x, y, 4, Resources.QuickDown,
    Resources.QuickLeft, Resources.QuickRight, Resources.QuickUp
    );
            tankList.Add(tank);
        }
        private static void CreateEnemyTank4(int x, int y)
        {
            EnemyTank tank = new EnemyTank(x, y, 1, Resources.SlowDown,
    Resources.SlowLeft, Resources.SlowRight, Resources.SlowUp
    );
            tankList.Add(tank);
        }
        // 生成地方坦克
        private static void EnemyBorn()
        {
            enemyBornCount++;
            if (enemyBornCount < enemyBornSpeed ) return;
            // 生成0-3的整数

            Random rd = new Random();
            int index = rd.Next(0, 3);
            Point postion = points[index];
            int enemyType = rd.Next(1, 5);
            switch (enemyType)
            {
                case 1:
                    CreateEnemyTank1(postion.X, postion.Y);
                    break;
                case 2:
                    CreateEnemyTank2(postion.X, postion.Y);
                    break;
                case 3:
                    CreateEnemyTank3(postion.X, postion.Y);
                    break;
                case 4:
                    CreateEnemyTank4(postion.X, postion.Y);
                    break;
            }
            enemyBornCount = 0;
          
        }
        // 绘制地图
        public static void DrawMap()
        {

            foreach (NotMoveing i in wallList)
            {

                i.Update();
            }
            foreach (NotMoveing i in seteelList)
            {

                i.Update();
            }
            foreach (NotMoveing i in bossList)
            {

                i.Update();
            }

            p.Update();

        }
        // 绘制墙
        private static void CreateWall(int x, int y, int count, List<NotMoveing> wallList, Image img)
        {
            int xp = x * 30;
            int yp = y * 30;
            for (int i = yp; i < yp + count * 30; i += 15)
            {
                NotMoveing left = new NotMoveing(xp, i, img);
                NotMoveing right = new NotMoveing(xp + 15, i, img);
                wallList.Add(left);
                wallList.Add(right);
            }

        }
        // 绘制boss集合
        private static void CreateBoss(int x, int y, List<NotMoveing> boosList, Image img)
        {
            NotMoveing p = new NotMoveing(x * 30, y * 30, img);
            boosList.Add(p);
        }
        //绘制我方坦克
        public static void myTank()
        {
            p = new MyTank(5 * 30, 14 * 30, 2, Direction.Up);


        }
        // 绘制坦克
        public static void DrawMyTank()
        {
            p.DrawSelf();

        }
        public static void KeyDown(KeyEventArgs e)
        {

            // keys是枚举类型
            p.KeyDown(e);
        }
        // 键盘抬起
        public static void KeyUp(KeyEventArgs e)
        {
            p.KeyUp(e);
        }
        // 检测碰撞体积
        public static NotMoveing IsCollidedWall(Rectangle rt)
        {
            foreach (NotMoveing wall in wallList)
            {
                // 判断墙的集合的碰撞体积是否和当前的坦克方式碰撞
                if (wall.GetRectangle().IntersectsWith(rt))
                {
                    return wall;
                }
            }
            return null;
        }
        // 检测钢墙
        public static NotMoveing IsCollidedSteel(Rectangle rt)
        {
            foreach (NotMoveing wall in seteelList)
            {
                // 判断墙的集合的碰撞体积是否和当前的坦克方式碰撞
                if (wall.GetRectangle().IntersectsWith(rt))
                {
                    return wall;
                }
            }
            return null;
        }
        public static bool IsCollidedBoss(Rectangle rt)
        {
            //foreach (NotMoveing wall in bossList)
            //{
            //    // 判断墙的集合的碰撞体积是否和当前的坦克方式碰撞
            //    if (wall.GetRectangle().IntersectsWith(rt))
            //    {
            //        return wall;
            //    }
            //}
            return bossList[0].GetRectangle().IntersectsWith(rt);
        }
        //创建子弹
        public static void CreateBullet(int x, int y, Tag tag, Direction dir)
        {
            Bullet p = new Bullet(x, y, 5, dir, tag);
            lock (_bulletLock)
            {
                bulletList.Add(p);
            }


        }
        // 销毁子弹
        public static void DestroyBullet(Bullet bullet)
        {
            lock (_bulletLock)
            {
                bulletList.Remove(bullet);
            }

        }
        //检查子弹是否被消除
        private static void CheckAndDestroyBullet()
        {
            List<Bullet> needToDestroy = new List<Bullet>();
            foreach (Bullet i in bulletList)
            {
                if (i.IsDestroy == true)
                {
                    needToDestroy.Add(i);
                }
            }
            foreach (Bullet i in needToDestroy)
            {
                bulletList.Remove(i);
            }

        }
        // 检查墙是否被销毁
        public static void DestroyWall(NotMoveing wall)
        {
            wallList.Remove(wall);
        }
        // 检查墙是否被销毁
        public static void DestroySeteel(NotMoveing wall)
        {
            seteelList.Remove(wall);
        }
        // 检测坦克是否被销毁
        //  // 检测是否与坦克发生了碰撞
        public static EnemyTank IsCollidedEnemyTank(Rectangle rt)
        {
            foreach (EnemyTank tank in tankList)
            {
                if (tank.GetRectangle().IntersectsWith(rt))
                {
                    return tank;
                }
            }
            return null;
        }
        // 检测敌方坦克是否攻击到主角
        public static MyTank IsCollidedMyTank(Rectangle rt)
        {
            if(p.GetRectangle().IntersectsWith(rt))
            {
                return p;
            }
            else
            {
                return null;
            }
        }
        // 摧毁坦克
        public static void DestroyTank(EnemyTank tank)
        {
            tankList.Remove(tank);
        }
        // 爆炸效果函数
        public static void CreateExplosion(int x, int y)
        {
            Explosion exp = new Explosion(x, y);
            expList.Add(exp);
        }
        // 消除爆炸效果
        private static void CheckAndDestroyExplosin()
        {
            /*
             * 这样做会越界，原因：
             * 集合不能一边生成一边删除
            foreach (Explosion i in expList)
            {
                if (i.IsOver == true)
                {
                    expList.Remove(i);
                }
            }
            return;
             
             */

            List<Explosion> needToDestroy = new List<Explosion>();
            foreach (Explosion i in expList)
            {
                if (i.IsOver == true)
                {
                    needToDestroy.Add(i);
                }
            }
            foreach (Explosion i in needToDestroy)
            {
                expList.Remove(i);
            }
        }
        public static void Overing()
        {
            foreach (Explosion i in expList)
            {
                //if(i.IsOver == true)
                //{
                //    expList.Remove(i);
                //}
            }
        }

    }
}
